//
//  ViewController.swift
//  swift-test
//
//  Created by apple on 2020/10/16.
//  Copyright © 2020 apple. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
	
	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view.
		view.backgroundColor = UIColor.green
		print("🌹：\(#function) in \(type(of: self))")
		view.addSubview(imageView)
		view.addSubview(yellowView)
		print("😊size:\(self.view.frame.size)")
	}
	
	lazy var yellowView: YellowView = {
		let view = YellowView(frame: CGRect(x: 0, y: 200, width: 100, height: 100))
		return view
	}()
	
	lazy var imageView: UIImageView = {
		let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
		imageView.backgroundColor = .red
		if let path = Bundle.main.path(forResource: "page_loading.gif", ofType: ""){
			let url=URL(fileURLWithPath: path)
		}
		imageView.image = UIImage(named: "page_loading.gif")
		return imageView
	}()
	
	override func viewDidLayoutSubviews() {
		print("😊：\(#function) in \(type(of: self))")
//		print(" \(#file)")
		print("SCREEN_WIDTH:\(UIScreen.main.bounds.size.width)")
		print("SCREEN_HEIGHT:\(UIScreen.main.bounds.size.height)")
	}

	override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//		testEatProtocol()
//		testCodable()
//		testProtocol()
//		testSelf()
//		testPSelf()
//		testIsEmpty()
//		testInit()
//		testIndex()
//		testThrow()
//		testEmpry()
//		testLayoutIfNeed()
//		addSub()
		chageSubview()
	}

}

extension ViewController {
	
	func chageSubview() {
		view.layoutIfNeeded()
//		yellowView.frame = CGRect(x: yellowView.frame.origin.x, y: yellowView.frame.origin.y, width: 200, height: 200)
		yellowView.view.frame = CGRect(x: yellowView.view.frame.origin.x, y: yellowView.view.frame.origin.y, width: 50, height: 50)
	}
	
	func addSub() {
		let subview = UIView(frame: CGRect(x: 200, y: 0, width: 100, height: 100))
		view.addSubview(subview)
		subview.backgroundColor = .blue
	}
	
	func testLayoutIfNeed() {
		view.layoutIfNeeded()
	}
	
	func testEmpry() {
		var str: String?
		str = nil
		if (str ?? "").isEmpty {
			print("😊空....")
		}
	}
	
	func testThrow() {
		do {
			let name = try throwTest(name: "fff ")
			print("\(name)")
		} catch {
			print("\(error)")
		}
	}
	
	func throwTest(name: String?) throws -> String {
		guard let n = name else {
			throw LSError.nilError
		}
		return n
	}
}

enum LSError: Error {
	case nilError
	case bigError
}

extension ViewController: SelfProtocol {
	
	private func testDict() {
		let dict: [String : Any] = ["a": "a",
					"b": "a",
					"c": "a",
					"d": "a",
					"e": "a",
					"name": "a",
					"age": 33,
					"f": "d",
					"g": "g",
					"h": "h"]
//		dict["a"] = "a"
//		dict["b"] = "a"
//		dict["c"] = "a"
//		dict["d"] = "a"
//		dict["e"] = "a"
	}
	
	private func testIndex() {
		let p1 = Person(name: "1")
		let p2 = Person(name: "2")
		let p3 = Person(name: "3")
		let array = [p1, p2, p3]
		if let index = array.firstIndex(where: {
			$0.name == "2"
		}) {
			print("\(index)")
		}
		
		let students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
		if let i = students.firstIndex(where: { $0.hasPrefix("A") }) {
			 print("\(students[i]) starts with 'A'!")
		}
	}
	
	private func testIsEmpty() {
		let a = ""
		print("\(a.isEmpty)")
	}
	
	private func testPSelf() {
		let p = Person()
		p.self.name = "sss"
		print("name:\(p.name ?? "")")
		
		Person.self.doSth()	// 类型的.self是元类型的值
		
//		let type1 = type(of: p)
//		let type2 = type(of: p.self)
//		let type3 = type(of: Person)
//		let type4 = type(of: Person.self)
//		let type5 = Person.Type.self
		
	}
	
	private func testSelf() {
		self.returnSelf().view.backgroundColor = .red
	}
	
	private func testProtocol() {
		let nextVC = NextViewController()
		self.present(nextVC, animated: true)
	}
	
	private func testInit() {
		let t = Person()
		t.name = ""
		
		var a = Animal()
		a.name = "1"
//		var a1 = Animal()
//		a1.name = ""
		print("nickName:\(a.nickName ?? "")")
		a.name = "2"
		print("nickName:\(a.nickName ?? "")")
	}
	
	private func testCodable() {
		let jsonData = """
					{
						"name": null,
						"age": 18
					}
		""".data(using: .utf8)!
		do {
			let model = try JSONDecoder().decode(CStruct.self, from: jsonData)
			print("\(model.name ?? "")---\(model.age ?? 0)")
		} catch {
			print("error:\(error)")
		}
	}
}

extension ViewController: AnotherEatProtocol {
	func testEatProtocol() {
		self.eat()
	}
}

struct CStruct: Codable {
	let name: String?
	let age: Int?
	
	func testSelf() -> Self {
		return self
	}
}

struct Animal: Codable{
	var name: String?
	var age: Int?
	var nickName: String? {
		get{
			name
		}
	}
}

class Person: NSCoding {
	// required
	func encode(with coder: NSCoder) { }
	
	required init?(coder: NSCoder) { }
	
	var name: String?
	init(name: String?) {
		self.name = name
	}
	required init() { }
	
	static func doSth() {
		print("做事情...")
	}
}

class Teacher: Person {
	var age: Int
	
	override init(name: String?) {
		self.age = 0
		super.init(name: name)
	}
	
	init(name: String?, age: Int) {
		self.age = age
		super.init(name: name)
	}
	
	required init() {
		fatalError("init() has not been implemented")
	}
	
	required init?(coder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
}

class LSView: UIView {
	init(frame: CGRect, name: String?) {
		super.init(frame: frame)
	}
	
	required init?(coder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	func doSth() {
		let name = TestEnum.first.name
		TestEnum.first.testFunc(name: name)
	}
}


protocol TestProtocol {
	var name: String { get }
}

extension TestProtocol {
	func testFunc(name: String) { }
}

enum TestEnum: TestProtocol {
	case first
	case second
}
extension TestEnum {
	var name: String {
		switch self {
		case .first:
			return "second"
		case .second:
			return "first"
		}
	}
}
